<?php
if (!defined('__COLA_DIR__')) exit;

/**
 * 编辑附件组件
 *
 * @author edikud
 * @package Widget
 * @copyright Copyright (c) 2019 Cola (http://www.mcooo.com)
 * @license GNU General Public License 2.0
 */
class Widget_Attachs_Edit extends Widget_Abstract_Attachs
{

    /**
     * 执行函数
     *
     * @throws Widget_Exception
     */
    public function execute()
    {
		/** 必须为注册者以上权限 */
        $this->user->pass('member');
		
        /** 更新模式 */
        if (!empty($this->request->aid) && 'delete' != $this->request->do ) {
            $this->select()
                ->where('aid = ?', $this->request->aid)
			    ->limit(1)
			    ->fetchRow(array($this, 'push'));
				
            if (!$this->have()) {
                throw new Widget_Exception(_t('附件不存在'), 404);
            }
        }
    }
	
    /**
     * 增加附件
     *
     * @access public
     * @return void
     */
    public function insertAttach($values = array())
    {
		/** 插件接口 */
		//HOOK $values | PLUG_Attach_Insert($values, $this);		
		
        /** 插入数据 */
        $insertId = $this->insert($values);
		
        if ($insertId > 0) {
		
		    /** 压入数据 */
		    $this->select()
                ->where('aid = ?', $insertId)
			    ->limit(1)
			    ->fetchRow(array($this, 'push'));

			/** 插件接口 */	
		    //HOOK PLUG_Attach_Insert_Success($this);
		
		}
		
		return $insertId;
    }
	
    /**
     * 更新附件
     *
     * @access public
     * @return void
     */
    public function updateAttach($values = array())
    {
		if (!$this->have()) {
		    return ;
		}
		
		/** 插件接口 */
		//HOOK $values | PLUG_Attach_Update($values, $this);
		
		$aid = $this->aid;
		
        /** 更新数据 */
        $updateRows = $this->update($aid, $values);
		
		/** 压入数据 */
        $this->select()
            ->where('aid = ?', $aid)
            ->limit(1)
		    ->fetchRow(array($this,'push'));

		/** 插件接口 */	
		//HOOK PLUG_Attach_Update_Success($this);
		
		return $updateRows;
    }

    /**
     * 删除附件
     *
     * @access public
     * @return void
     */
    public function deleteAttach($aid)
    {
        $deleteRows = false;
		$attach = $this->select()
			->where('aid = ?', $aid)
			->fetchRow(array($this, 'push'));

        if ($attach) {
			
			/** 插件接口 */	
		    //HOOK {$plugged} PLUG_Attachs_Delete($this);

            if (!$plugged) {
                !Common::isAppEngine() && @unlink(Common::url(Widget_Upload::$uploadPath, __COLA_DIR__) . $this->filePath);
            }
			
			/** 删除 */
		    $deleteRows = $this->delete($aid);

			/** 插件接口 */	
		    //HOOK PLUG_Attachs_Delete_Success($this);

        }

		return $deleteRows;
    }
	
    protected function eachList($type, $id)
    {
        $attachlist = $this->db->select()
		    ->from('table.attachs')
			->where('type = ?', $type)
            ->where('id = ?', 0)
		    ->where('uid = ?', $this->user->uid)
			->fetchAll();
		
        if($id){
			$oldlist = $this->db->select()
			    ->from('table.attachs')
			    ->where('type = ?', $type)
                ->where('id = ?', $id)
			    ->fetchAll();
		    $oldlist && $attachlist = array_merge($attachlist, $oldlist);	
		}
		return $attachlist;
    }
	
    /**
     * 同步附件
     *
     * @access public
     * @param string $type
     * @param integer $id
     * @return array
     */
    public function sync($type, $id)
    {
        $images = $files = 0;
        $attachlist = $this->eachList($type, $id);

        foreach($attachlist as $attach) 
		{
			!$attach['id'] && $this->db->update('table.attachs')
		        ->where('aid = ?', $attach['aid'])
		        ->values(array('id'=>$id))
		        ->affectedRows();
			
            if ($attach['isImage']) {
				++$images;
			} else {
				++$files;
			}
		}
		
		return array($images, $files);
    }
	
    /**
     * 取消附件关联
     *
     * @access public
     * @param integer $cid
     * @return void
     */
    public function remove($type, $id)
    {
        $attachlist = $this->eachList($type, $id);
			
		/** 删除 */
	    foreach($attachlist as $value) 
		{
			$this->deleteAttach($value['aid']);
		}
    }

}

